package com.keson.blue;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Constant {
    public static final String defaultApiUrl = "http://47.94.80.3:8080";
	public static final String topic = "quake";
    public static final String eqrTopic = "eqr";
	public static final int qos = 2;
	public static String broker;
	public static String clientNo;
	public static String typeId = "4";
	public static String orgId;
	public static String version;
	public static String appName;
	public static String updateUrl;
	public static String adUpdateUrl;
	public static int countDown;
	public static int alertLight;
	public static int alertSound;
	public static int emergencyLamp;
	public static int relay;
	public static float delayTime;
	public static String ntpServer;
	public static String openTime;
	public static String closeTime;
	public static boolean cloud;
	public static double longitude;
	public static double latitude;
	public static boolean lock;
	public static String centerName;
	public static int intensity;
	public static int adVolume;
	public static int alertVolume;
	public static int intensityReturn;
	public static String orgName;
	public static boolean makeVideo;
	public static String uploadVideoUrl;
	public static boolean audioHDMI;
	public static boolean broadcast;
	public static String broadcastUrl;
    public static String versionUrl;
	public static boolean h24;
    public static boolean autoUpdateLog;
    public static String color1;
    public static String color2;
    public static String color3;
    public static String color4;
    public static int luminance;
    public static String hz;
    public static float autoCheckTime;
    public static int alertDisplayMode;
    public static float updateRate;

	public static final String onlineTopic  = "online";
	/*msg -1 δ֪ ��1���ߣ� 0����*/
	public static String onlineMsg;
	public static String offlineMsg;
	public static final int onlineQos = 2;
	
	public static final String actionTopic  = "action";
	public static final int actionQos = 2;

    public static final String answerTopic = "answer";
    public static final int answerQos = 2;

	public static final String intensityTopic  = "intensity";
	public static final int intensityQos = 2;
	
	public static String weatherUrl = "http://wthrcdn.etouch.cn/weather_mini?city=";
	public static String city;
	public static String cityKey;
	public static String weatherAlertUrl = "http://d1.weather.com.cn/dingzhi/";
    public static String lockPsw;
    public static int receive;
    public static String stations;
    public static String replayLockPsw;
    public static String memsIp;
    public static boolean enableRF433 = true;
    public static boolean enableAcc = false;
    public static boolean enableTF;
    public static boolean enableHandset;
    public static boolean hideBattery;
    public static boolean enableRealKey;
    public static boolean brightnessPositive;
    public static String clientName;

    private static JSONObject config;
	private static JSONObject info;
	private static String configFileName = Environment.getExternalStorageDirectory().toString()+"/config.properties";
	private static String infoFileName = Environment.getExternalStorageDirectory().toString()+"/info.properties";
	
//	烈度灵敏度
	public static float I0;
	public static float I1;
	public static float I2;
	public static float I3;
	public static float I4;
	public static float I5;
	public static float I6;
	public static float I7;
	public static float I8;
	public static float I9;
	public static float I10;
	public static float I11;
    public static Context context;

    public static boolean regCenter;

//    设备控制  false为树莓派
    public static boolean devControlEnable = true;
//    是否为视美泰
//    public static boolean smt = false;
//    主板类型
    public static String board = Build.MODEL;
//    注册中心使用消息类型四川局为false
    public static boolean isJson = false;

    //老版本灯板为10
    public static final int ReceiveDataLength = 11;

	public static void readConfig() {
	    System.out.println("配置文件：读取配置");
        Properties prop = new Properties();
//        try {
//            String config_file = "config.properties";
//            InputStream in = applicationContext.getAssets().open(config_file);  //打开assets目录下的config.properties文件
//            prop.load( new InputStreamReader(in,"utf-8"));
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        config = JSONObject.parseObject(prop.getProperty("config"));

//        try {
//            String config_file = "info.properties";
//            InputStream in = applicationContext.getAssets().open(config_file);  //打开assets目录下的config.properties文件
//            prop.load( new InputStreamReader(in,"utf-8"));
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        info = JSONObject.parseObject(prop.getProperty("info"));

        File configFile = new File(configFileName);
        File infoFile = new File(infoFileName);

        FileReader is = null;
        BufferedReader br = null;
        String inf = "";
        try {
            is = new FileReader(configFile);
            br = new BufferedReader(is);
            String cfg = "";
            String line = "";
            while((line = br.readLine()) != null) {
                cfg = cfg + line;
            }
            config = JSONObject.parseObject(cfg);

            is = new FileReader(infoFile);
            br = new BufferedReader(is);
            inf = br.readLine();
            info = JSONObject.parseObject(inf);

            Constant.broker = config.getString("broker");
            Constant.appName = config.getString("appName");
            Constant.clientNo = info.getString("clientNo");
            Constant.typeId = info.getString("typeId");
            Constant.orgId = info.getString("orgId");
            Constant.version = config.getString("version");
            Constant.countDown = config.getIntValue("countDown");
            Constant.alertLight = config.getIntValue("alertLight");
            Constant.alertSound = config.getIntValue("alertSound");
            Constant.emergencyLamp = config.getIntValue("emergencyLamp");
            Constant.relay = config.getIntValue("relay");
            Constant.delayTime = config.getFloatValue("delayTime");
            Constant.ntpServer = config.getString("ntpServer");
            Constant.openTime = config.getString("openTime");
            Constant.closeTime = config.getString("closeTime");
            Constant.cloud = config.getBooleanValue("cloud");
            Constant.longitude = config.getDoubleValue("longitude");
            Constant.latitude = config.getDoubleValue("latitude");
            Constant.lock = config.getBooleanValue("lock");
            Constant.intensity = config.getIntValue("intensity");
            Constant.centerName = config.getString("centerName");
            Constant.adVolume = config.getIntValue("adVolume");
            Constant.alertVolume = config.getIntValue("alertVolume");
            Constant.updateUrl = config.getString("updateUrl");
            Constant.adUpdateUrl = config.getString("adUpdateUrl");
            Constant.intensityReturn = config.getIntValue("intensityReturn");
            Constant.orgName = config.getString("orgName");
            Constant.alertDisplayMode = config.getIntValue("alertDisplayMode");
            Constant.isJson = config.getBooleanValue("isJson");
            Constant.lockPsw = config.getString("lockPsw");
            Constant.replayLockPsw = config.getString("replayLockPsw");
            Constant.memsIp = config.getString("memsIp");
            Constant.receive = config.getIntValue("receive");
            Constant.stations = config.getString("stations");
            Constant.clientName = config.getString("clientName");

            JSONObject intensityInterval = config.getJSONObject("intensityInterval");
            if(intensityInterval!=null){
                Constant.I0 = intensityInterval.getFloatValue("0");
                Constant.I1 = intensityInterval.getFloatValue("1");
                Constant.I2 = intensityInterval.getFloatValue("2");
                Constant.I3 = intensityInterval.getFloatValue("3");
                Constant.I4 = intensityInterval.getFloatValue("4");
                Constant.I5 = intensityInterval.getFloatValue("5");
                Constant.I6 = intensityInterval.getFloatValue("6");
                Constant.I7 = intensityInterval.getFloatValue("7");
                Constant.I8 = intensityInterval.getFloatValue("8");
                Constant.I9 = intensityInterval.getFloatValue("9");
                Constant.I10 = intensityInterval.getFloatValue("10");
                Constant.I11 = intensityInterval.getFloatValue("11");
            }

            Constant.makeVideo = config.getBooleanValue("makeVideo");
            Constant.uploadVideoUrl = config.getString("uploadVideoUrl");
            Constant.audioHDMI = config.getBooleanValue("audioHDMI");
            Constant.broadcast = config.getBooleanValue("broadcast");
            Constant.broadcastUrl = config.getString("broadcastUrl");
            Constant.versionUrl = config.getString("versionUrl");
            Constant.h24 = config.getBooleanValue("h24");
            Constant.autoUpdateLog = config.getBooleanValue("autoUpdateLog");
            Constant.color1 = config.getString("color1");
            Constant.color2 = config.getString("color2");
            Constant.color3 = config.getString("color3");
            Constant.color4 = config.getString("color4");
            Constant.luminance = config.getIntValue("luminance");
            Constant.hz = config.getString("hz");
            Constant.autoCheckTime = config.getFloatValue("autoCheckTime");
            Constant.updateRate = config.getFloatValue("updateRate")==0?4:config.getFloatValue("updateRate");
            Constant.enableRF433 = config.getBooleanValue("enableRF433");
            Constant.enableAcc = config.getBooleanValue("enableAcc");
            Constant.enableTF = config.getBooleanValue("enableTF");
            Constant.enableHandset = config.getBooleanValue("enableHandset");
            Constant.hideBattery = config.getBooleanValue("hideBattery");
            Constant.enableRealKey = config.getBooleanValue("enableRealKey");
            Constant.brightnessPositive  = config.getBooleanValue("brightnessPositive");

            Constant.onlineMsg = "{clientNo:'"+ Constant.clientNo+"',orgId:'"+ Constant.orgId+"',type:"+ Constant.typeId+",version:'"+ Constant.version+"',msg:'1'}";
            Constant.offlineMsg = "{clientNo:'"+ Constant.clientNo+"',msg:'0'}";
            Constant.cityKey = info.getString("city");
            String cityName = CityKeyUtil.getCityName(info.getString("city"));
            Constant.city = cityName==null?"北京":cityName;

            Constant.regCenter = config.getBooleanValue("regCenter");
        } catch (Exception e) {
            System.out.println("配置文件错误尝试使用默认配置连接mqtt");
            Constant.broker = "tcp://47.94.80.3:61613";
            Constant.appName = "eqplayer";
            String clientId = inf.substring(inf.indexOf("{\"clientNo\":\"")+"{\"clientNo\":\"".length());
            Constant.clientNo = clientId.substring(0,clientId.indexOf("\",\""));
            Constant.typeId = "4";
            Constant.orgId = "1";
            Constant.version = "1";
            Constant.countDown = 3;
            Constant.alertLight = 3;
            Constant.alertSound = 3;
            Constant.emergencyLamp = 3;
            Constant.relay = 3;
            Constant.delayTime = 2;
            Constant.ntpServer = "ntp1.aliyun.com";
            Constant.openTime = "06:30:00";
            Constant.closeTime = "18:30:00";
            Constant.cloud = false;
            Constant.longitude = 120;
            Constant.latitude = 36;
            Constant.lock = false;
            Constant.intensity = 6;
            Constant.centerName = "sichuan";
            Constant.adVolume = 0;
            Constant.alertVolume = 0;
            Constant.updateUrl = "http://47.94.80.3:8080/update";
            Constant.intensityReturn = 6;
            Constant.orgName = "";
            Constant.alertDisplayMode = 1;
            Constant.isJson = false;
            Constant.lockPsw = "";
            Constant.replayLockPsw = "";
            Constant.memsIp = "";
            Constant.receive = 1;
            Constant.stations = "";
            Constant.clientName = "";

            Constant.makeVideo = false;
            Constant.broadcast = false;
            Constant.h24 = false;
            Constant.autoUpdateLog = false;
            Constant.color1 = "R00,G00,B10";
            Constant.color2 = "R00,G00,B10";
            Constant.color3 = "R00,G00,B10";
            Constant.color4 = "R00,G00,B10";
            Constant.luminance = 10;
            Constant.hz = "05";
            Constant.autoCheckTime = -1;
            Constant.updateRate = 4;
            Constant.enableRF433 = false;
            Constant.enableAcc = false;
            Constant.enableTF = false;
            Constant.enableHandset = false;
            Constant.hideBattery = false;
            Constant.enableRealKey = false;
            Constant.brightnessPositive = false;

            Constant.onlineMsg = "{clientNo:'"+ Constant.clientNo+"',orgId:'"+ Constant.orgId+"',type:"+ Constant.typeId+",version:'"+ Constant.version+"',msg:'1'}";
            Constant.offlineMsg = "{clientNo:'"+ Constant.clientNo+"',msg:'0'}";
            Constant.cityKey = "101170101";
            String cityName = CityKeyUtil.getCityName(info.getString("city"));
            Constant.city = cityName==null?"北京":cityName;

            Constant.regCenter = false;
        }
    }
	
	public static JSONObject getConfigJsonObject() {
		return config;
	}
	
	public static JSONObject getInfoJsonObject() {
		return info;
	}
	
	public static void updateConfigToFile(JSONObject config, JSONObject info) {
        System.out.println("配置文件：写入配置");
        //�ѱ��ذ汾�ļ�����Ϊ����ͬ��
		FileOutputStream verOS1 = null;  
        FileOutputStream verOS2 = null;  
        try {  
            verOS1 = new FileOutputStream(configFileName);
            verOS1.write(config.toJSONString().getBytes());
            
            verOS2 = new FileOutputStream(infoFileName);  
            verOS2.write(info.toJSONString().getBytes());
            
        } catch (IOException ex) {  
        } finally {  
            try {  
                verOS1.close();
                verOS2.close(); 
            } catch (IOException ex1) {  
            }  
        }  
    }

    public static void parseCheck(JSONObject config, JSONObject info) throws Exception {
        config.getString("broker");
        config.getString("appName");
        info.getString("clientNo");
        info.getIntValue("typeId");
        info.getIntValue("orgId");
        config.getString("version");
        config.getIntValue("countDown");
        config.getIntValue("alertLight");
        config.getIntValue("alertSound");
        config.getIntValue("emergencyLamp");
        config.getIntValue("relay");
        config.getFloat("delayTime");
        config.getString("ntpServer");
        config.getString("openTime");
        config.getString("closeTime");
        config.getBooleanValue("cloud");
        config.getDoubleValue("longitude");
        config.getDoubleValue("latitude");
        config.getBooleanValue("lock");
        config.getIntValue("intensity");
        config.getString("centerName");
        config.getIntValue("adVolume");
        config.getIntValue("alertVolume");
        config.getString("updateUrl");
        config.getString("adUpdateUrl");
        config.getIntValue("intensityReturn");
        config.getString("orgName");
        config.getIntValue("alertDisplayMode");
//        config.getBooleanValue("smt");
        config.getBooleanValue("isJson");
        config.getString("lockPsw");
        config.getString("replayLockPsw");
        config.getString("memsIp");
        config.getIntValue("receive");
        config.getString("stations");
        config.getString("clientName");

        JSONObject intensityInterval = config.getJSONObject("intensityInterval");
        if(intensityInterval!=null){
            intensityInterval.getFloatValue("0");
            intensityInterval.getFloatValue("1");
            intensityInterval.getFloatValue("2");
            intensityInterval.getFloatValue("3");
            intensityInterval.getFloatValue("4");
            intensityInterval.getFloatValue("5");
            intensityInterval.getFloatValue("6");
            intensityInterval.getFloatValue("7");
            intensityInterval.getFloatValue("8");
            intensityInterval.getFloatValue("9");
            intensityInterval.getFloatValue("10");
            intensityInterval.getFloatValue("11");
        }

        config.getBooleanValue("makeVideo");
        config.getString("uploadVideoUrl");
        config.getBooleanValue("audioHDMI");
        config.getBooleanValue("broadcast");
        config.getString("broadcastUrl");
        config.getString("versionUrl");
        config.getBooleanValue("h24");
        config.getBooleanValue("autoUpdateLog");
        config.getString("color1");
        config.getString("color2");
        config.getString("color3");
        config.getString("color4");
        config.getIntValue("luminance");
        config.getString("hz");
        config.getFloat("autoCheckTime");
        config.getBooleanValue("regCenter");
        info.getString("city");
        config.getFloatValue("updateRate");
        config.getBooleanValue("enableRF433");
        config.getBooleanValue("enableAcc");
        config.getBooleanValue("enableTF");
        config.getBooleanValue("enableHandset");
        config.getBooleanValue("hideBattery");
        config.getBooleanValue("enableRealKey");
        config.getBooleanValue("brightnessPositive");
    }
}
